package com.softeem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.softeem.model.Reservoir;
import com.softeem.service.IReservoirService;

@Controller
@RequestMapping("/reservoir")
public class ReservoirController {
	@Autowired
	private IReservoirService  reservoirService; 
	
	 /*
     * 用户列表与分页Action
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(Model model, Reservoir r){
    
    	
    
    	Map<String,Object> result=new HashMap<String, Object>();
    	int num=reservoirService.queryReservoirCount();
        List<Reservoir> rows=new ArrayList<Reservoir>();
        int totol=num;
        int page=r.getPage();
        if(page==1) {
        	r.setPage(0);
        }else {
        	   r.setPage((page-1)*r.getLimit());
        }
     
        
      
        
        rows=reservoirService.queryReservoirs(r);
    	result.put("rows",rows);
    	result.put("total",num);
    	result.put("status",200);
    	result.put("msg","获取数据成功");
    	
     
        return result;
    }
    
    
    @RequestMapping(value ="/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(@RequestBody Reservoir r,HttpServletRequest req){
    	Map<String,Object> result=new HashMap<String, Object>();
    	String ss= (String) req.getAttribute("size");
    	String tt=(String) req.getAttribute("id");
    	if(r!=null) {
    		reservoirService.insertReservoir(r);
        	result.put("status",200);
        	result.put("msg","添加数据成功");
    	}else {
    		result.put("status",500);
        	result.put("msg","添加数据失败");
    	}
    	
    	
    	
     
        return result;
    }
    
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(@RequestBody Reservoir r){
    	reservoirService.updateReservoir(r);
    	Map<String,Object> result=new HashMap<String, Object>();
    	result.put("status",200);
    	result.put("msg","添加数据成功");
        return result;
    }
    
    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(@RequestParam("id") int id){
    	reservoirService.deleteReservoirById(id);
    	Map<String,Object> result=new HashMap<String, Object>();
    	result.put("status",200);
    	result.put("msg","删除数据成功");
        return result;
    }
    
    
    
   
    
    
    
	
	

}
